import 'package:date_format/date_format.dart';
import 'public_chineseDateLocale.dart';

enum DateType {
  YY,
  YYMM,
  YYMMDD,
  YYMMDDHHNN,
  YYMMDDHHNNSS,
  MMDD,
}

class HLDate {
  /*
  * 矫正时间
  * */
  static String hl_formatDate(String? date) {
    if (date == null || date.isEmpty) {
      return "";
    }
    return formatDate(
      DateTime.parse(date),
      [yyyy, '-', mm, '-', dd, ' ', HH, ':', nn, ":", ss],
      locale: const ChineseDateLocale(),
    );
  }

  /*
  * 时间转换
  * */
  static String hl_dateTimeToString({DateTime? date, DateType type = DateType.YYMMDDHHNNSS}) {
    return formatDate(
      date ?? DateTime.now(),
      _formatType(type: type),
      locale: const ChineseDateLocale(),
    );
  }

  /*
  * 格式化时间
  * */
  static String hl_format({String? date, DateType type = DateType.YYMMDDHHNNSS, List<String>? format}) {
    DateTime dateTime;
    if (date == null || date.isEmpty) {
      dateTime = DateTime.now();
    } else {
      dateTime = DateTime.parse(date);
    }

    if (format != null && format.isNotEmpty) {
      return formatDate(dateTime, format);
    }

    List<String> formats = _formatType(type: type);
    return formatDate(dateTime, formats, locale: ChineseDateLocale());
  }

  /*
  * 字符串转时间DateTime
  * */
  static DateTime hl_stringToDateTime(String? date) {
    return (date != null) ? DateTime.parse(date) : DateTime.now();
  }

  /*
  * 字符串转时间DateTime
  * */
  static DateTime hl_yymmddToDateTime({int? year, int? month, int? day}) {
    return (year != null && month != null && day != null) ? DateTime(year, month, day) : DateTime.now();
  }

  /*
  * 格式类型
  * */
  static List<String> _formatType({DateType type = DateType.YYMMDDHHNNSS}) {
    List<String> formats = [];
    switch (type) {
      case DateType.YYMMDDHHNNSS:
        formats = [yyyy, '-', mm, '-', dd, ' ', HH, ':', nn, ":", ss];
        break;
      case DateType.YY:
        formats = [yyyy];
        break;
      case DateType.YYMM:
        formats = [yyyy, '-', mm];
        break;
      case DateType.YYMMDD:
        formats = [yyyy, '-', mm, '-', dd];
        break;
      case DateType.YYMMDDHHNN:
        formats = [yyyy, '-', mm, '-', dd, ' ', HH, ':', nn];
        break;
      case DateType.MMDD:
        formats = [mm, '-', dd];
        break;
    }
    return formats;
  }
}
